﻿using AdminSystem.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics.Metrics;
using System.Linq.Expressions;

namespace AdminSystem.Repositories
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        public Repository(AdminSystemDbContext db)
        {
            Db = db;
        }

        public AdminSystemDbContext Db { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Insert(TEntity entity)
        {
            Db.Set<TEntity>().Add(entity);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> AddRange(IEnumerable<TEntity> entities)
        {
            Db.Set<TEntity>().AddRange(entities);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> Delete(int id)
        {
            Db.Set<TEntity>().Remove(Db.Set<TEntity>().Find(id));
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            Db.Set<TEntity>().RemoveRange(Db.Set<TEntity>().Find(ids));
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> GetAll()
        {
            return await Db.Set<TEntity>().ToListAsync();
        }

        /// <summary>
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns
        /// 根据条件查询多条
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await Db.Set<TEntity>().Where(predicate).ToListAsync();
        }

        //public async Task<IList<TEntity>> GetAll(Expression<Func<TEntity, bool>> expression)
        //{
        //    return await Db.Set<TEntity>().Where(expression).ToListAsync();
        //}

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TEntity> GetById(int id)
        {
            return await Db.Set<TEntity>().FindAsync(id);
        }

        /// <summary>
        /// 根据条件查询是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await Db.Set<TEntity>().AnyAsync(predicate);
        }

        public async Task<int> Update(TEntity entity)
        {
            Db.Set<TEntity>().Update(entity);
            return await Db.SaveChangesAsync();
        }

        public async Task<int> UpdateRange(IEnumerable<TEntity> entities)
        {
            Db.Set<TEntity>().UpdateRange(entities);
            return await Db.SaveChangesAsync();
        }

        // 查询一条数据的方法
        public async Task<TEntity> QueryFirstAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await Db.Set<TEntity>().FirstOrDefaultAsync(predicate);
        }

    }
}